<?php

Class Brute extends Connect{
    
    var $pet;# = array();
    private $user;
    
    /**
     * Накормить питомца
    */
    function food($user, $element){
        $storage = new Storage();
        #return  $storage->getOne($user, 'food', $element);
        $food =  $storage->getOne($user, 'food', $element) ;
        if( !is_array($food) ){
            $this->_error("Нет такой еды");
            return false;    
        }
        Goods::getAll();
        if( !$m = Goods::$all['food'][$food['parent']] ) return false;
        #return $food['food'];
        
        #return $food;          
        /*
        Habars::getElement($element);
        if(Habars::$elements[$element]['type'] != 1){
            $this->_error("Питомец траву не ест");
            return false;    
        }
        */
        $this->getPet($user);
        #if( !$this->setPractic(Habars::$elements[$element]['practic']) ) {
        $this->setLevel($m['setlevel']);        
        if( !$this->setPractic($m['practic']) ) {
            $this->_error("Не удалось набрать опыт");
            return false;    
        } 
        if( !$this->save() ) return false;    
         
        
        if( !$storage->del($user, 'food', $element) ) return false;
        return true;
    }
    
    /**
     * Защита
     * $user = id, 
     * $steal = array
    */
    function protection($user, $steal){
        #return $steal;
        $this->getPet($user);
        $chance = new Chance();
        Level::getAllPractic($this->pet['levelpet']);
        Level::getAllPractic($steal['level']);
        #return Level::$practic;
        #return $this->pet;
        $arr = array(
            $user => Level::$practic[$this->pet['levelpet']] + $this->pet['practicpet'],
            $steal['kontaktid'] => Level::$practic[$steal['level']] + $steal['practic'], 
        );
        #return $arr;
        #return $chance->getToPercent($arr);
        $id = $chance->get($chance->getToPercent($arr));
        #return $id;
        $this->pet['steals'] = $this->pet['steals'] + 1; 
        if( $id == $user){
            #Поймал вора
            #+ Опыт 10% опыта вора
            $this->setPractic( ( ( Level::$practic[$steal['level']] + $steal['practic'] )/100)*10 );
            $this->pet['stealspet'] = $this->pet['stealspet'] + 1;
            $this->save();
            return true;
        }else{
            #Не поймал
            $this->save();
            return false;
        }
    }
    
    /**
     * своровать
     * $user = id, 
     * $steal = array
    */
    function steal($user, $stel){        
        $this->getPet($user);        
        $this->pet['steals'] = $this->pet['steals'] + 1;         
        $this->save();
        return true;        
    }
    /**
     * поймать 
    */
    function prot($user, $steal){                
        $this->getPet($user);        
        Level::getAllPractic($steal['level']);
        #Поймал вора
        #+ Опыт 10% опыта вора
        $this->setPractic( intval(( ( $steal['practic'] )/100)*10) );
        $this->pet['stealspet'] = $this->pet['stealspet'] + 1;         
        $this->pet['steals'] = $this->pet['steals'] + 1;
        $this->save();
        Messages::send($user, "dragonAttck", $steal['kontaktid'] );
        return true;        
    }
    
    /**
     * 
     * Возвращает зверюгу
    */
    function getPet($id){                
        /*if(is_array($_SESSION['brute'][$id]) && count($_SESSION['brute'][$id]) > 0 ){
            $this->pet = $_SESSION['brute'][$id];
            return true;
        }*/         
        $this->user = $id; 
        $sql = "SELECT `pet`, `levelpet`, `practicpet`, `steals`, `stealspet` FROM 
                `".PREFIX_TABLE."users` WHERE `kontaktid` = '".$id."' LIMIT 1";
        
        #return $sql;
        if( !$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false;
        }
        $this->pet = DBConnect::getOne($result);        
        /*        
        $pets = Pet::getPets();                
        $this->pet['name'] = $pets[$this->pet['pet']]['name'];
        $this->pet['descript'] = $pets[$this->pet['pet']]['descript'];
        #$this->pet['level'] = $user['levelpet'];
        #$arr['image'] = MEDIA_URL.$pets[$user['pet']]['skins'][$user['levelpet']]['image']; 
        
        $this->pet['image'] = $pets[$this->pet['pet']]['skins'][$this->pet['levelpet']]['image'];
        $this->pet['anime'] = $pets[$this->pet['pet']]['skins'][$this->pet['levelpet']]['anime'];
        $this->pet['thumbimage'] = $pets[$this->pet['pet']]['skins'][$this->pet['levelpet']]['thumbimage'];
        
        if(floatval($this->pet['steals']) == 0){
            $this->pet['efect'] = 0;    
        }else{
            $this->pet['efect'] = floatval($this->pet['stealspet'])/( floatval($this->pet['steals'])/100 );    
        }
        */
        $this->pet['progress'] = Level::getPercent(intval($this->pet['levelpet'])+1, $this->pet['practicpet']); 
        #$_SESSION['brute'][$id] = $this->pet;      
        return true;
    }
    
    private function setLevel($level){
        if($level > 0){
            $oldL = $this->pet['levelpet']; 
            $this->pet['levelpet'] = min(100,  intval($this->pet['levelpet'] + $level) );
            $step = 20; #100/5;
            if( intval($oldL/$step) < intval($this->pet['levelpet']/$step) ){
                Messages::send($this->user, "Питомцем открыт уровень ".intval($this->pet['levelpet']/$step) );
            }                
        } 
    }
    
    private function setPractic($practic){        
        $this->pet['practicpet'] = $this->pet['practicpet'] + $practic;
        Level::getPractic($this->pet['levelpet'] + 1);
        if(Level::$levels[$this->pet['levelpet'] + 1] <= $this->pet['practicpet']){
            $this->setLevel(1);
            #$this->pet['levelpet'] = $this->pet['levelpet'] + 1;
            $this->pet['practicpet'] = intval($this->pet['practicpet']) - intval(Level::$levels[$this->pet['levelpet']]);
            #Messages::send($this->user, "Питомцем открыт уровень ".$this->pet['levelpet']);
        }
        #Messages::send($this->user, "Питомцем Заработан опыт ".$this->pet['practicpet']);
        return true;
    }
    
    private function save(){
        $sql = "UPDATE `".PREFIX_TABLE."users` set ";
        ##`name` = 'pet1',`descript` = 'pet1',`level` = '',`image` = 'http://192.168.6.194/media/pznahar/pets/pet.png',`thumbimage` = ''
        $arr = $this->pet;
        unset($arr['name']);
        unset($arr['descript']);
        unset($arr['level']);
        unset($arr['image']);
        unset($arr['thumbimage']);
        unset($arr['efect']);
        unset($arr['progress']);
        unset($arr['anime']);
        $_SESSION['brute'] = $arr; 
        
        $i = 1;              
        foreach($arr as $key => $val){
            $sql .= "`".$key."` = '".$val."'";
            $sql .= ($i < count($arr))? "," : "" ;
            $i++;    
        }
        $sql .= "  WHERE `kontaktid` = '".$this->user."' LIMIT 1";
        
        #return $sql;
        if(!$result = DBConnect::query($sql)){
            $this->_error(mysql_error());
            return false; 
        }
        return true;
    }      
}
?>